package com.example.tiku2;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tiku2.adapter.MainProjectAdapter;
import com.example.tiku2.bean.ProjectBean;
import com.example.tiku2.presenter.NetPresenter;
import com.example.tiku2.view.NetView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements NetView {

    private RecyclerView rv_main;
    private ArrayList<ProjectBean.DataBean.DatasBean> list;
    private MainProjectAdapter adapter;
    private NetPresenter netPresenter;
    private MyBroadcastService myBroadcastService;
    private TextView tv_title;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initRv();
        netPresenter = new NetPresenter(this);
        netPresenter.getData();
    }

    private void initRv() {
        rv_main.setLayoutManager(new LinearLayoutManager(this));
        rv_main.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        list = new ArrayList<>();
        adapter = new MainProjectAdapter(this, list);
        rv_main.setAdapter(adapter);

        adapter.setOnItemClickLis(new MainProjectAdapter.OnItemClickLis() {
            @Override
            public void onItemClick(int i) {
                Intent intent = new Intent("myBroadcastService");
                ProjectBean.DataBean.DatasBean datasBean = list.get(i);
                Intent it = intent.putExtra("title", datasBean.getTitle());
                ArrayList<ProjectBean.DataBean.DatasBean> beans = new ArrayList<>();
                beans.addAll(list);
                it.putExtra("list", beans);
                sendBroadcast(it);
            }
        });
    }

    private void initView() {
        rv_main = (RecyclerView) findViewById(R.id.rv_main);
        tv_title = (TextView) findViewById(R.id.tv_title);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
-
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setData(List<ProjectBean.DataBean.DatasBean> datas) {
        list.addAll(datas);
        adapter.notifyDataSetChanged();
    }

    //动态注册广播
    @Override
    protected void onResume() {
        super.onResume();
        myBroadcastService = new MyBroadcastService();
        IntentFilter filter = new IntentFilter("myBroadcastService");
        registerReceiver(myBroadcastService, filter);
    }

    //解除注册
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastService);
    }
}
